home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / program / t_bcinfo.zip / TI655.ZIP / TI655.ASC
Text File  |  1992-02-25  |  1KB  |  67 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C                              NUMBER  :  655
  9.   VERSION  :  All
  10.        OS  :  PC DOS
  11.      DATE  :  February 25, 1992                        PAGE  :  1/1
  12.  
  13.     TITLE  :  Getting the Offset of a Member of a Structure
  14.  
  15.  
  16.  
  17.  
  18.  
  19.   How to get the offset of a member of a structure:
  20.  
  21.   #include <stddef.h>
  22.  
  23.   struct mys {
  24.      int member1;
  25.      int member2;
  26.   };
  27.  
  28.   int where_b = offsetof( struct mys , member2 );
  29.  
  30.   What this looks like is this:
  31.  
  32.   int where_b = (size_t)&(((struct mys _FAR *)0)->member2);
  33.  
  34.   What this does is typecast absolute address 0 to be a far pointer
  35.   to the struct.   Then  it references member2 off of this address.
  36.   Then we take the address of this location. Because 0 is cast to a
  37.   far  pointer,  we  are  starting  at 0000:0000 so the address  of
  38.   member2  will  be  the  relative  offset of a member2 within  any
  39.   structure. All this can be evaluated at compile time.
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.